﻿using System.Web.Mvc;
using EBE.COM.Core.Interfaces;
using EBE.COM.Core.Models;
using Newtonsoft.Json;

namespace EBE.COM.Web.Areas.Admin.Controllers
{
  public class CategoriesController : Controller
  {
    // todo: inject a factory for category creation

    private readonly IRepository<ICategory> _repository;

    public CategoriesController(IRepository<ICategory> repository)
    {
      // inject the UOW instead of the repository
      _repository = repository;
    }

    public ActionResult Index()
    {
      return View();
    }

    public string GetCategories()
    {
      var list = _repository.FindAll();
      return JsonConvert.SerializeObject(list);
    }

    public string AddCategory(string name)
    {
      _repository.Add(new Category { Name = name });
      // todo: add some logic in here
      return JsonConvert.SerializeObject(true);
    }

    public string RemoveCategory(int id)
    {
      _repository.Remove(new Category { Id = id });
      // todo: add some logic in here
      return JsonConvert.SerializeObject(true);
    }

    public string GetCategory(int id)
    {
      var category = _repository.FindById(id);
      return JsonConvert.SerializeObject(category);
    }

    public string UpdateCategory(int id, string name)
    {
      var category = new Category { Id = id, Name = name };
      _repository.Update(category);
      // todo: add some logic in here
      return JsonConvert.SerializeObject(true);
    }
  }
}
